/*
 * @Author: song
 * @Description:技能编译器界面   TIP---------------- _config是 编辑运行中的实例  需要保存 不然会丢失
 * @Date: 2024年05月03日 星期五 22:05:42
 * @Modify:
 */

using System.Collections.Generic;
using Function.SkillEditor.Editor;
using SCore.Editor.WindowDraw;
using SCore.Util;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;

namespace Function.SkillEditor.Editor
{
    public enum SkillWindowType
    {
        Main = 0,
        Config = 1,
        Camera = 2,
        TimeLine = 3,
        Inspector = 4,
    }

    public class SkillEditorWindow : IEditorWindow
    {
        public static EventManager EventManager = new EventManager(); //事件
        public static SkillEditorConfig _config; //编译器配置
        private string _sceneSave; //上一个的场景路径

        [MenuItem("SCore/Function/SkillEditor")]
        private static void OpenDebuggerWindow()
        {
            SkillEditorWindow editorWindow = (SkillEditorWindow)EditorWindow.GetWindow(typeof(SkillEditorWindow));
            editorWindow.titleContent = new GUIContent("SkillEditorWindow");
            editorWindow.minSize = new Vector2(1500, 960);
            editorWindow.maximized = true;
            editorWindow.Show();
        }


        public override void OnEnable()
        {
            RefreshConfig();
            _sceneSave = EditorSceneManager.GetActiveScene().path;
            EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo();
            EditorSceneManager.NewScene(NewSceneSetup.DefaultGameObjects);
            base.OnEnable();
        }

        public override void OnDisable()
        {
            if (!string.IsNullOrEmpty(_sceneSave))
                EditorSceneManager.OpenScene(_sceneSave);
            base.OnDisable();
        }

        public override void OnGUI()
        {
            IESubWindow ieSubIESubWindow = null;
            Rect area = Rect.zero;
            // 使用水平布局分隔左中右三个大区域
            using (new GUILayout.HorizontalScope(EditorDrawHelp.GetBoxStyle(EditorDrawHelp._ivory4),
                       GUILayout.ExpandWidth(true),
                       GUILayout.ExpandHeight(true)))
            {
                using (new GUILayout.VerticalScope("box", GUILayout.MinWidth(150),
                           GUILayout.ExpandHeight(true)))
                {
                    ieSubIESubWindow = GetWindowView((int)SkillWindowType.Config);
                    ieSubIESubWindow.OnGUI();
                }

                UpdateSubWindowRect(ieSubIESubWindow);


                using (new GUILayout.VerticalScope("box",
                           GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true)))
                {
                    using (new GUILayout.VerticalScope(
                               GUILayout.ExpandWidth(true), GUILayout.MinHeight(600)))
                    {
                        ieSubIESubWindow = GetWindowView((int)SkillWindowType.Camera);
                        ieSubIESubWindow.OnGUI();
                    }

                    UpdateSubWindowRect(ieSubIESubWindow);


                    using (new GUILayout.VerticalScope("box",
                               GUILayout.ExpandWidth(true),
                               GUILayout.ExpandHeight(true)))
                    {
                        ieSubIESubWindow = GetWindowView((int)SkillWindowType.TimeLine);
                        ieSubIESubWindow.OnGUI();
                    }

                    UpdateSubWindowRect(ieSubIESubWindow);
                }

                using (new GUILayout.VerticalScope("box", GUILayout.MaxWidth(200),
                           GUILayout.ExpandHeight(true)))
                {
                    ieSubIESubWindow = GetWindowView((int)SkillWindowType.Inspector);
                    ieSubIESubWindow.OnGUI();
                }

                UpdateSubWindowRect(ieSubIESubWindow);
            }

            base.OnGUI();
        }

        protected override Dictionary<int, IESubWindow> BindWindow()
        {
            var window = new Dictionary<int, IESubWindow>();
            window.Add((int)SkillWindowType.Config, new SkillConfigSubWindow(this));
            window.Add((int)SkillWindowType.Camera, new SkillCameraSubWindow(this));
            window.Add((int)SkillWindowType.TimeLine, new SkillTimeLineSubWindow(this));
            window.Add((int)SkillWindowType.Inspector, new SkillInspectorSubWindow(this));
            return window;
        }

        protected override List<ECommonView> BindView()
        {
            var windowConfig = SkillEditorConfig.Instance.GetWindowConfig(SkillWindowType.Main);
            if (windowConfig != null && windowConfig._eCommonViews.Count > 0)
            {
                foreach (var eCommonView in windowConfig._eCommonViews)
                {
                    eCommonView.SetUp(this);
                }

                return windowConfig._eCommonViews;
            }

            return base.BindView();
        }

        public void RefreshConfig()
        {
            _config = Instantiate(SkillEditorConfig.Instance);
        }

        private void UpdateSubWindowRect(IESubWindow ieSubIESubWindow)
        {
            var area = GUILayoutUtility.GetLastRect();
            if (area != Rect.zero && area != new Rect(0, 0, 1, 1))
            {
                ieSubIESubWindow.UpdateArea(area);
            }
        }
    }
}